using System.Collections.Generic;
using Avalonia.Interactivity;
using FluentAvalonia.UI.Controls;
using FluentAvalonia.UI.Windowing;
using LuckyFish.CalculatorFluent.ViewModels.Pages;

namespace LuckyFish.CalculatorFluent.Views;

public partial class MainWindow : AppWindow
{
    private Dictionary<string, PageModelBase> Stack { get; }

    public MainWindow()
    {
        InitializeComponent();

        Stack = new Dictionary<string, PageModelBase>
        {
            { "Home", new HomeViewModel() },
            { "Calendar", new CalendarViewModel() },
            { "Setting", new SettingViewModel() },
            { "Help", new HelpViewModel() }
        };

        TitleBar.ExtendsContentIntoTitleBar = true;
        TitleBar.TitleBarHitTestType = TitleBarHitTestType.Complex;

        FrameView.IsNavigationStackEnabled = true;
        FrameView.NavigationPageFactory = new NavigationFactory();
        Navigate("Home");
    }

    #region Item

    private void ItemInvoked(object? sender, NavigationViewItemInvokedEventArgs e)
    {
        if (e.InvokedItemContainer.Tag is not string s) return;
        if (string.IsNullOrEmpty(s)) return;
        Navigate(s);
    }

    private void Navigate(string page)
    {
        if (string.IsNullOrEmpty(page)) return;
        Navigate(Stack[page]);
    }

    private void Navigate(object context)
    {
        FrameView.NavigateFromObject(context);
    }

    private void BackClick(object? sender, RoutedEventArgs e)
    {
        FrameView.GoBack();
    }

    #endregion
}